package com.frxuexi.mdm2204

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.frxuexi.mdm2204.demo.Demo01
import com.frxuexi.mdm2204.demo.Demo02
import com.frxuexi.mdm2204.demo.Demo03
import com.frxuexi.mdm2204.demo.Demo04
import com.frxuexi.mdm2204.demo.Demo05
import com.frxuexi.mdm2204.demo.Demo06
import com.frxuexi.mdm2204.ui.theme.MDM2204Theme
import com.frxuexi.mdm2204.view.MainPage
import com.frxuexi.mdm2204.view.WelcomePage
import com.tencent.mmkv.MMKV
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestPermissions(arrayOf<String>("android.permission.READ_EXTERNAL_STORAGE"),100)
        setContent {
            MDM2204Theme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
//                   var i= 5/0

//                    Demo06()
                     val kv = MMKV.defaultMMKV()
 //                    kv.encode("myname","zs")

 //                    val name = kv.decodeString("myname", "")
 //                    Log.i("===",name!!)

                     //                    1.创建 NavController
                     val navController = rememberNavController()
                     var page="welcome"
                     if(kv.decodeBool("isIn",false)){
                         page="main"
                     }/*else{
                         lifecycleScope.launch {
                             async { delay(3000) }.await()
                             kv.encode("isIn",true)
                             navController.navigate("welcome")
                         }
                     }*/

 //                    2.创建 NavHost
                     NavHost(navController = navController, startDestination = page){
                         /*composable("demo03"){ Demo03(navController)}
                        composable("demo04/{name}"){
                            val name = it.arguments?.getString("name", "")
                            Demo04(name!!)}
                        composable("demo05"){ Demo05() }*/
                        composable("welcome"){ WelcomePage(navController)}
                        composable("main"){ MainPage()}
                    }


                }
            }
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putString("name","zs")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        savedInstanceState.getString("name","")
    }
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
    Text(
        text = "Hello $name!",
        modifier = modifier,
        textAlign = TextAlign.Center
    )
}


@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MDM2204Theme {
//        Greeting("world")
        Demo01()
    }
}